package com.mls.fivedistrict.controller.backend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.enums.GoodsOrderStatusEnum;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.GoodsOrder;
import com.mls.fivedistrict.server.GoodsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author chenhua
 * @date 2018/5/18 13:49
 */
@RestController("backendGoodsOrderController")
@RequestMapping("/api/manager")
public class GoodsOrderController extends BaseController {
    @Autowired
    GoodsOrderService goodsOrderService;

    /**
     * 订单发货
     * @param goodsOrderId
     * @return
     */
    @PutMapping("/v1/shipGoodsOrder/{goodsOrderId}")
    public ServiceResponse shipGoodsOrder(@PathVariable Long goodsOrderId, @RequestBody GoodsOrder goodsOrder){
        Optional.ofNullable(goodsOrderId).orElseThrow(() -> new CustomerException("发货订单id不能为空"));
        Optional.ofNullable(goodsOrder).orElseThrow(() -> new CustomerException("发货订单内容不能为空"));
        Optional.ofNullable(goodsOrder.getTrackingName()).orElseThrow(() -> new CustomerException("物流公司名称不能为空"));
        Optional.ofNullable(goodsOrder.getTrackingNumber()).orElseThrow(() -> new CustomerException("物流单号不能为空"));
        GoodsOrder order = goodsOrderService.selectById(goodsOrderId);
        if(order.getStatus()==GoodsOrderStatusEnum.UNSHIPPED){
            goodsOrder.setVersion(order.getVersion());
            goodsOrder.setId(goodsOrderId);
            goodsOrder.setStatus(GoodsOrderStatusEnum.SHIPPED);
        }else {
            return ServiceResponse.createErrorResponse("订单状态为待发货时才能发货");
        }
        boolean flag = goodsOrderService.updateById(goodsOrder);
        if (flag){
            return ServiceResponse.createSuccessResponse("发货成功");
        }else {
            return ServiceResponse.createErrorResponse("发货失败");
        }
    }

    /**
     * 修改订单
     * @param goodsOrderId
     * @param goodsOrder
     * @return
     */
    @PutMapping("/v1/goodsOrder/{goodsOrderId}")
    public ServiceResponse editGoodsOrder(@PathVariable Long goodsOrderId, @RequestBody GoodsOrder goodsOrder){
        Optional.ofNullable(goodsOrderId).orElseThrow(() -> new CustomerException("修改订单id不能为空"));
        Optional.ofNullable(goodsOrder).orElseThrow(() -> new CustomerException("修改订单不能为空"));
        if(goodsOrder.getStatus()==GoodsOrderStatusEnum.SHIPPED){
            return ServiceResponse.createErrorResponse("订单已发货无法修改");
        }
        if(goodsOrder.getStatus()==GoodsOrderStatusEnum.COMPLETED){
            return ServiceResponse.createErrorResponse("订单已完成无法修改");
        }
        if(goodsOrder.getStatus()==GoodsOrderStatusEnum.EXPIRED){
            return ServiceResponse.createErrorResponse("订单已失效无法修改");
        }
        goodsOrder.setId(goodsOrderId);
        boolean flag = goodsOrderService.updateById(goodsOrder);
        if (flag){
            return ServiceResponse.createSuccessResponse("修改订单成功");
        }else {
            return ServiceResponse.createErrorResponse("修改订单失败");
        }
    }

    /**
     * 删除订单
     * @param goodsOrderId
     * @return
     */
    @DeleteMapping("/v1/goodsOrder/{goodsOrderId}")
    public ServiceResponse deleteGoodsOrder(@PathVariable Long goodsOrderId){
        Optional.ofNullable(goodsOrderId).orElseThrow(() -> new CustomerException("删除订单id不能为空"));
        GoodsOrder goodsOrder = new GoodsOrder();
        goodsOrder.setId(goodsOrderId);
        goodsOrder.setIsDelete(true);
        boolean flag = goodsOrderService.updateById(goodsOrder);
        if(flag){
            return ServiceResponse.createSuccessResponse("删除订单成功");
        }else{
            return ServiceResponse.createErrorResponse("删除订单失败");
        }
    }

    /**
     * 订单列表
     * @return
     */
    @GetMapping("/v1/goodsOrders")
    public ServiceResponse getGoodsOrderList(){
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(),pageFilter.getPageSize());
        List list = goodsOrderService.selectList(pageFilter.getWrapper());
        PageInfo<GoodsOrder> goodsOrderPageInfo = new PageInfo<GoodsOrder>(list, 5);
        return  ServiceResponse.createSuccessResponse(goodsOrderPageInfo);
    }

    /**
     * 根据id查询订单
     * @param goodsOrderId
     * @return
     */
    @GetMapping("/v1/goodsOrder/{goodsOrderId}")
    public ServiceResponse getGoodsOrder(@PathVariable Long goodsOrderId){
        Optional.ofNullable(goodsOrderId).orElseThrow(() -> new CustomerException("查询订单id不能为空"));
        GoodsOrder goodsOrder = goodsOrderService.selectById(goodsOrderId);
        if (null!=goodsOrder){
            return ServiceResponse.createSuccessResponse(goodsOrder);
        }else{
            return ServiceResponse.createErrorResponse("该订单不存在");
        }
    }
}
